﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data
{
    /// <summary>
    /// 用于封装sql语句中的Like参数
    /// </summary>
    public sealed class LikeParameter
    {
        public LikeParameter(string value, LikeMode mode = LikeMode.Contains)
        {
            Mode = mode;
            Value = value;
        }
        /// <summary>
        /// 搜索模式
        /// </summary>
        public LikeMode Mode { get; set; } = LikeMode.Contains;
        /// <summary>
        /// 搜索值
        /// </summary>
        public string Value { get; set; }

        public static implicit operator LikeParameter(string str)
        {
            if (str == null)
            {
                return null;
            }
            return new LikeParameter(str);
        }

        public override string ToString()
        {
            if (Value == null)
            {
                return null;
            }
            string temp = "{0}";
            switch (Mode)
            {
                case LikeMode.Contains:
                    temp = "%{0}%";
                    break;
                case LikeMode.StartWith:
                    temp = "{0}%";
                    break;
                case LikeMode.EndWith:
                    temp = "%{0}";
                    break;
                default:
                    break;
            }
            return string.Format(temp, Value);
        }
    }
    /// <summary>
    /// 搜索模式
    /// </summary>
    public enum LikeMode
    {
        /// <summary>
        /// 包含
        /// </summary>
        Contains,
        /// <summary>
        /// 搜索开头
        /// </summary>
        StartWith,
        /// <summary>
        /// 搜索结尾
        /// </summary>
        EndWith
    }
}
